<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
%>

<!DOCTYPE HTML>
<html>
	<head>
		<title></title>
		<meta http-equiv="pragma" content="no-cache" />
		<meta http-equiv="cache-control" content="no-cache"/>
		<meta http-equiv="expires" content="0" />
		
		<link rel="stylesheet" href="<%=path %>/scripts/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css" />
		
		<script type="text/javascript" src="<%=path %>/scripts/jquery/jquery-1.8.2.min.js"></script>
		<script type="text/javascript" src="<%=path %>/scripts/jquery/jquery.json-2.2.min.js"></script>
		<script type="text/javascript" src="<%=path %>/scripts/zTree/jquery.ztree.core-3.0.min.js"></script>
		<script type="text/javascript" src="<%=path %>/scripts/zTree/jquery.ztree.buttons.js"></script>
		<script type="text/javascript" src="<%=path %>/scripts/jquery/jquery.SuperSlide.2.1.1.js"></script>
  		<script type="text/javascript">
			$.ajaxSetup({cache:false,async:false});
			var setting = {
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					onClick:zTreeOnClick
				}
			};
			var deptAndUserMaker = function(){
				var nodes = [];
				$.getJSON("<%=path %>/org/List",function(data){
					if(data != null){
						$.each(data,function(i,item){
							var h = {id:item.id,name:item.orgAlias,code:item.id,open:false,icon:"<%=path %>/scripts/zTree/css/zTreeStyle/img/diy/1_close.png"};
							nodes.push(h);
						});
					}
				});
				$.getJSON("<%=path %>/dept/List",function(data){
					if(data != null){
						$.each(data,function(i,item){
							var d = {id:item.id,pId:item.defaultOrg,name:item.deptAlias,code:"dept"};
							nodes.push(d);
						});
					}
				});
				$.getJSON("<%=path %>/user/List",function(data){
					if(data != null){
						var p = {id:"0",name:"其他",open:false,icon:"<%=path %>/scripts/zTree/css/zTreeStyle/img/diy/1_close.png"};
						nodes.push(p);
						$.each(data,function(i,item){
							var defaultDept = item.defaultDeptCode;
							if(defaultDept != null){
								var arrs = defaultDept.split(",");
								$.each(arrs,function(i,o){
									var d = {id:item.id,pId:o,name:item.realName+"["+item.account+"]",code:"user"};
									nodes.push(d);
								});
							}else{
								var d = {id:item.id,pId:"0",name:item.realName+"["+item.account+"]",code:"user"};
								nodes.push(d);
							}
						});
					}
				});
				return nodes;
			};
			var postMaker = function(){
				var nodes = [];
				$.getJSON("<%=path %>/org/List",function(data){
					if(data != null){
						$.each(data,function(i,item){
							var h = {id:item.id,name:item.orgAlias,code:item.id,open:false,icon:"<%=path %>/scripts/zTree/css/zTreeStyle/img/diy/1_close.png"};
							nodes.push(h);
							$.getJSON("<%=path %>/position/List?org="+item.id,function(post){
								if(post != null){
									$.each(post,function(i,o){
										var d = {id:o.id,pId:o.defaultOrg,name:o.postName,code:"post"};
										nodes.push(d);
									});
								}
							});
						});
					}
				});
				return nodes;
			};
			var groupMaker = function() {
				var nodes = [];
				var p = {id:"0",name:"群组",open:true,icon:"<%=path %>/scripts/zTree/css/zTreeStyle/img/diy/1_close.png"};
				nodes.push(p);
				$.getJSON("<%=path %>/group/List",function(data){
					if(data != null){
						$.each(data,function(i,item){
							var d = {id:item.id,pId:"0",name:item.groupName,code:"group"};
							nodes.push(d);
						});
					}
				});
				return nodes;
			};
			var zNodes = deptAndUserMaker();
			var selMaker = function(maker){
				if(maker == "deptUser"){
					zNodes = deptAndUserMaker();
				}
				if(maker == "post"){
					zNodes = postMaker();
				}
				if(maker == "group"){
					zNodes = groupMaker();
				}
				$("ul.sub>li").removeClass("on");
				$("ul.sub>#"+maker+"").addClass("on");
				$.fn.zTree.init($("#relationTree"), setting, zNodes);
			};
			function zTreeOnClick(event, treeId, treeNode) {
				if(treeNode.pId != null){
					var state = true;
					var sel = $("#defaultMultiple");
					$.each(sel.get(0).options,function(i,o){
						if(o.value.indexOf(treeNode.id) != -1){
							state = false;
						}
					});
					if(state){
						if(treeNode.code == "dept"){
							sel.get(0).options.add(new Option("[部门]"+treeNode.name+"", "[dept]"+treeNode.id+""));
						}
						if(treeNode.code == "user"){
							sel.get(0).options.add(new Option("[用户]"+treeNode.name+"", "[user]"+treeNode.id+""));
						}
						if(treeNode.code == "group"){
							sel.get(0).options.add(new Option("[群组]"+treeNode.name+"", "[group]"+treeNode.id+""));
						}
						if(treeNode.code == "post"){
							sel.get(0).options.add(new Option("[职位]"+treeNode.name+"", "[post]"+treeNode.id+""));
						}
					}
				}
			}
			$(function(){
				$.fn.zTree.init($("#relationTree"), setting, zNodes);
				var opts = {
					relation:"relation",
					code:"relationCode",
					name:"relationName",
					multiple:"defaultMultiple",
					submitBtn:"btnSubmit",
					removeBtn:"btnRemove",
					closeBtn:"btnClose"
				};
				closeClick(opts);
				removeClick(opts);
				submitClick(opts);
				loadMultiple(opts);
			});
		</script>
	</head>

	<body>
		<div class="left_sel">
			<div class="treebar">选择项</div>
			<div class="sel_nav">
				<ul class="sub">
					<li id="deptUser" onclick="selMaker('deptUser');" class="on"><a href="#">部门&用户</a></li>
					<li id="post" onclick="selMaker('post');"><a href="#">职位</a></li>
					<li id="group" onclick="selMaker('group');"><a href="#">群组</a></li>
				</ul>
			</div>
			<div class="content_wrap">
				<ul id="relationTree" class="ztree"></ul>
			</div>
		</div>
		<div class="operation">
			<div class="treebar">操作</div>
			<div class="win_btn" id="btnRemove">移 除</div>
			<div class="win_btn" id="btnSubmit">确 定</div>
			<div class="win_btn" id="btnClose">关 闭</div>
		</div>
		<div class="right_sel">
			<div class="treebar">已选项</div>
			<select multiple="multiple" id="defaultMultiple" class="select_multiple"></select>
		</div>
	</body>
</html>
